package com.wsc.wsckeeper.utils;

import android.app.Dialog;
import android.content.Context;
import android.text.Html;
import android.view.View;
import android.widget.TextView;

import com.wsc.wsckeeper.R;
import com.wsc.wsckeeper.app.KApplication;
import com.wsc.wsckeeper.managers.ResourceManager;

public class DialogUtils {

	public static void createOkDialog(Context context, String title, String message, final Runnable ok) {
		final Dialog dialog = new Dialog(context);
		dialog.setCancelable(false);
		dialog.setContentView(R.layout.dialog_ok);
		dialog.setTitle(title);

		((TextView) dialog.findViewById(R.id.message)).setText(message);

		dialog.findViewById(R.id.ok).setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if (ok != null) {
					ok.run();
				}
				dialog.cancel();
			}
		});

		dialog.show();
	}

	public static void confirmActionDialog(Context context, String message, final Runnable ok) {
		final Dialog dialog = new Dialog(context);
		dialog.setCancelable(false);
		dialog.setContentView(R.layout.dialog_confirm_action);
		dialog.setTitle(ResourceManager.getStringValue(R.string.dialog_confirn_action));

		TextView text = (TextView) dialog.findViewById(R.id.action_message);
		text.setText(Html.fromHtml(message));

		dialog.findViewById(R.id.action_yes).setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if (ok != null) {
					ok.run();
				}
				dialog.cancel();
			}
		});

		dialog.findViewById(R.id.action_no).setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				dialog.cancel();
			}
		});

		dialog.show();
	}

	public static void showException(final Context context, final Exception e) {
		if (e.getMessage() != null)
			showException(context, e.getMessage());
		else
			showException(context, e.toString());
	}

	public static void showException(final Context context, final String message) {
		KApplication.handler.post(new Runnable() {
			public void run() {
				DialogUtils.createOkDialog(context, ResourceManager.getStringValue(R.string.dialog_title_error), message, null);
			}
		});
	}
}
